<?php
/* 
 * Additionnal functions
 */
//require_once("../../config.php");

//Translate error code
function getError($return){
    switch($return){
        case 12 : $return = "Command line parameter error.";
            break;
        case 11 : $return = "Plist error.";
            break;
        case 20 : $return = "Server unavailable.";
            break;
        case 21 : $return = "Server hostname lookup failure.";
            break;
        case 22 : $return = "Server timed out.";
            break;
        case 23 : $return = "Server is not running SSL.";
            break;
        case 24 : $return = "Server certificate is not valid.";
            break;
        case 25 : $return = "Server bad response.";
            break;
        case 26 : $return = "Server authorization failure";
            break;
        case 30 : $return = "Submission failed.";
            break;
        case 31 : $return = "Upload server unavailable.";
            break;
        case 32 : $return = "File upload failed.";
            break;
        case 33 : $return = "File copy failed.";
            break;
        case 34 : $return = "Submission complete failed.";
            break;
        case 35 : $return = "Agent bind failed on the server.";
            break;
        case 36 : $return = "Agent s command failed on the server.";
            break;
        case 37 : $return = "An unknow server error has occured.";
            break;
        case 255 : $return = "Something is broken, check username, password and RSA key.";
            break;
        }
   return $return;
}

//Fonction qui renvoie si on a le droit de faire une action sur les contexte systeme, module, ou course
function is_allowed($cap, $courseid = 0, $instanceid = 0){

$contextc = get_context_instance(CONTEXT_COURSE, $courseid);
$contexti = get_context_instance(CONTEXT_MODULE, $instanceid);
$contexts = get_context_instance(CONTEXT_SYSTEM, 0);

/*print_object($contexts);
print_object($contextc);
print_object($contexti);*/

$allowed = 0;

if(has_capability($cap, $contextc) || has_capability($cap, $contexts)|| has_capability($cap, $contexti))
	$allowed = 1;

//echo $cap."->".$allowed."-".has_capability($cap, $contexts)."-".has_capability($cap, $contextc)."-".has_capability($cap, $contexti)."<br>";

return $allowed;
}

function parseWorkflows(&$doc, &$message, &$output){
    global $CFG;
    $parser = new plistParser();
    $plist = $parser->parseRaw($output);
    foreach($plist["workflows"] as $wf) {
       if(strstr($wf['name'], 'moodle') != false){
           $wfi = $doc->createElement( "workflow" );
           $message->appendChild($wfi);
           //Nom, description...
           $name = $doc->createElement( "name" );
           $name->appendChild( $doc->createTextNode( $wf['name'] ));
           $wfi->appendChild($name);
           $title = $doc->createElement( "title" );
           $title->appendChild( $doc->createTextNode( $wf['title']));
           $wfi->appendChild($title);
           $description = $doc->createElement( "description" );
           $description->appendChild( $doc->createTextNode( $wf['description'] ));
           $wfi->appendChild($description);
           $uuid = $doc->createElement( "uuid" );
           $uuid->appendChild( $doc->createTextNode( $wf['uuid'] ));
           $wfi->appendChild($uuid);
           //User requirements
           $ur = $doc->createElement( "user_requirements" );
           $i=0;
           foreach($wf['user_requirements'] as $uri) {
               $urii[$i] = $doc->createElement( 'require' );
               $urii[$i]->appendChild( $doc->createTextNode( $uri ));
               $ur->appendChild($urii[$i]);
               $i++;
           }
           $wfi->appendChild($ur);
       }
    }
}
?>
